import BasicLayout from '@/layouts/basic-layout';
const files = require.context('../pages', true, /router\.config\.js$/);
const Routes = [];
files.keys().forEach(key => {
    let _R = files(key).router;
    Routes.push(_R)
});
/**
 * 在主框架内显示
 */
let getRouterPath = (menuInfo) => {
    let authName = ''
    let path = ''
    const token = icu.cookies.get('Authorization');
    if (!token) {
        return '/login'
    }
    if (menuInfo && menuInfo.menuHeader.length) {
        authName = menuInfo.menuHeader[0].name;
    } else {
        return null
    }
    if (menuInfo && menuInfo.menuSider.length) {
        for (let i = 0; i < menuInfo.menuSider.length; i++) {
            const siderItem = menuInfo.menuSider[i];
            if (authName == siderItem.header) {
                if (siderItem.children && siderItem.children.length) {
                    path = siderItem.children[0].path;
                } else {
                    path = siderItem.path;
                }
                return path;
            }
        }
    } else {
        return '/403'
    }
}
const frameIn = [
    {
        path: '/',
        name: 'main',
        // redirect: {
        //     name: 'Home'
        // },
        redirect: to => {
            let $menuinfo = {
                menuHeader: icu.storage.getData('menuHeader'),
                menuSider: icu.storage.getData('menuSider')
            }
            if (!icu.storage.getData('menuHeader')) {
                return {
                    path: '/root/home'
                }
            }
            return {
                path: getRouterPath($menuinfo) ? getRouterPath($menuinfo) : '/403'
            }
        },
        component: BasicLayout,
        children: [
            // 刷新页面 必须保留
            {
                path: 'refresh',
                name: 'refresh',
                hidden: true,
                component: {
                    beforeRouteEnter(to, from, next) {
                        next(instance => instance.$router.replace(from.fullPath));
                    },
                    render: h => h()
                }
            },
            // 页面重定向 必须保留
            {
                path: 'redirect/:route*',
                name: 'redirect',
                hidden: true,
                component: {
                    beforeRouteEnter(to, from, next) {
                        next(instance => instance.$router.replace(JSON.parse(from.params.route)));
                    },
                    render: h => h()
                }
            }
        ]
    },
].concat(Routes);
/**
 * 在主框架之外显示
 */

const frameOut = [
    {
        path: '/root/home',
        name: 'root-home',
        meta: {
            auth: true,
            title: '主系统'
        },
        component: () => import('@/pages/account/rootHome/index.vue')
    },
    {
        path: '/center/fontApp',
        name: 'center-fontapp',
        meta: {
            auth: true,
        },
        redirect: {
            name: 'center-fontapp-content-index'
        },
        component: () => import('@/pages/fontApp.vue'),
        children: [
            {
                path: '/content/index',
                name: 'center-fontapp-content-index',
                meta: {
                    auth: true,
                    title: '系统中心'
                },
                component: () => import('@/pages/fontApp/content.vue'),
            },
            {
                path: '/detailView/index/:type',
                name: 'center-fontapp-detailView-index',
                meta: {
                    auth: true,
                    title: '查看更多'
                },
                component: () => import('@/pages/fontApp/detialView/index.vue'),
            },
            {
                path: '/detailInfo/index/:type',
                name: 'center-fontapp-detailInfo-index',
                meta: {
                    auth: true,
                    title: '详细信息'
                },
                component: () => import('@/pages/fontApp/detialView/detail.vue'),
            }
        ]
    },
    // 登录
    {
        path: '/login',
        name: 'login',
        meta: {
            title: '$t:page.login.title'
        },
        component: () => import('@/pages/account/login')
    },
    {
        path: '/commodityLogin',
        name: 'commodityLogin',
        meta: {
            title: '$t:page.commodityLogin.title'
        },
        component: () => import('@/pages/account/commodityLogin')
    },
    // 注册
    {
        path: '/register',
        name: 'register',
        meta: {
            title: '$t:page.register.title'
        },
        component: () => import('@/pages/account/register')
    },
    // 注册结果
    {
        path: '/register/result',
        name: 'register-result',
        meta: {
            auth: true,
            title: '注册结果'
        },
        component: () => import('@/pages/account/register/result')
    }
];

/**
 * 错误页面
 */

const errorPage = [
    {
        path: '/403',
        name: '403',
        meta: {
            title: '403'
        },
        component: () => import('@/pages/system/error/403')
    },
    {
        path: '/500',
        name: '500',
        meta: {
            title: '500'
        },
        component: () => import('@/pages/system/error/500')
    },
    {
        path: '*',
        name: '404',
        meta: {
            title: '404'
        },
        component: () => import('@/pages/system/error/404')
    }
];

// 导出需要显示菜单的
export const frameInRoutes = frameIn;

// 重新组织后导出
export default [
    ...frameIn,
    ...frameOut,
    ...errorPage
];
